package com.lq.test;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class BusinessTest {

    public static void main(String[] args) {
		
		Map<String,List<Raw>> rawDB = new RawDB().getRawDB();
		Map<String,List<Production>> productionDB = new ProductionDB().getProductionDB();
		String productName = CODE.PRODUCTION_NO206;
		List<Production> productionList = productionDB.get(productName);
		
		List<List<Raw>> materials = new ArrayList<List<Raw>>();
		List<Raw> produceRaws = new ArrayList<Raw>();
		for(Production temp : productionList){
			String rawName = temp.getRawName();
			List<Raw> listRaw = rawDB.get(rawName);
			for(Raw raw : listRaw) 
				raw.setProvideDaily(raw.getProvideDaily()/temp.getAmount());
			materials.add(listRaw);
		}
		Integer added = 1;
		List<List<Integer>> sequs = calc(materials,added);
		for(List<Integer> seq : sequs){
			System.out.println(seq);
			for(int i = 0 ;i < seq.size(); i++){
				produceRaws.add(materials.get(i).get(seq.get(i)));
			}
			DataBean data = compareListRaws(produceRaws);
			if(data != null){
				System.out.println("-----------");
				System.out.println(productName + " "+ formatDate(data.getStartDate()) + 
						" "+formatDate(data.getEndDate()) + " "+ data.getAmount());
				System.out.println("-----------");
			}
			//for(Raw raw : produceRaws)
			//System.out.println(productName + " " + raw.getRawName() + "　" +
			//		raw.getStartDate()+" " + raw.getEndDate() + " " + raw.getProvideDaily());
			produceRaws.removeAll(produceRaws);
		}
		
	}
	
	public static String formatDate(Date date){
		SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return simple.format(date);
	}
	
	public static List<List<Integer>> calc(List<List<Raw>> list,Integer added){
		List<Integer> max = new ArrayList<Integer>();
		List<Integer> start = new ArrayList<Integer>();
		List<Integer> init = new ArrayList<Integer>();
		for(List<Raw> rawT : list){
			int sizeT = rawT.size();
			max.add(sizeT);
			start.add(0);
			init.add(0);
		}
		List<List<Integer>>  a = new ArrayList<List<Integer>>();
		a.add(init);
		while(!CalculateWestly.isOverflow(start,max,added)){
			a.add(CalculateWestly.calcAdd(start,max,added));
		}
		return a;
		
	}
	
	//
	public static DataBean compareListRaws(List<Raw> listRaw){
		DataBean result = null;
		int size = listRaw.size();
		List<Date> startDate = new ArrayList<Date>();
		List<Date> endDate = new ArrayList<Date>();
		List<Integer> amountList = new ArrayList<Integer>();
		for(int i = 0 ;i < size; i++){
			startDate.add(listRaw.get(i).getStartDate());
			endDate.add(listRaw.get(i).getEndDate());
			amountList.add(listRaw.get(i).getProvideDaily().intValue());
			
		}
		result = compareListDate(startDate,endDate);
		if(result != null){
			int amount = 0;
			for(int i=0 ;i<amountList.size()-1;i++){
				int rawi = amountList.get(i).intValue();
				int rawii = amountList.get(i+1).intValue();
				if(rawi <= rawii ){
					amount = rawi;
				}else
					amount = rawii;
			}
			result.setAmount(amount);
		}
		return result;
	}
	//比较两组时间的重合
	public static DataBean compareListDate(List<Date> startList,List<Date> endList){
		
		if(startList == null || endList == null || 
				startList.size() == 0 || endList.size() == 0){
			return null;
		}
		long st = startList.get(0).getTime();
		long ed = endList.get(0).getTime();
		for(Date startDate : startList){
			if(st <= startDate.getTime()){
				st = startDate.getTime();
			}
		}
		for(Date endDate : endList){
			if(ed >= endDate.getTime()){
				ed = endDate.getTime();
			}
		}
		if(st <= ed){
			DataBean result = new DataBean();
			result.setStartDate(new Date(st));
			result.setEndDate(new Date(ed));
			return result;
		}else
			return null;
	}
	//比较两个原料时间并将两个原料更新为新时间/未使用
	public static DataBean compareRaws(Raw raw1,Raw raw2){
		Date startDate1 = raw1.getStartDate();
		Date endDate1 = raw1.getEndDate();
		Date startDate2 = raw2.getStartDate();
		Date endDate2 = raw2.getEndDate();
		DataBean result = new DataBean();
		if(startDate1 == null || endDate1 == null ||
				startDate2 == null || endDate2 == null){
			return null;
		}
		long st1 = startDate1.getTime();
		long ed1 = endDate1.getTime();
		long st2 = startDate2.getTime();
		long ed2 = endDate2.getTime();
		
		//(st1-[st2-ed2]-ed1)
		if(st1 <= st2 && ed1 >=ed2 ){
			result.setStartDate(new Date(st2));
			result.setEndDate(new Date(ed2));
			
		}
		//[st2-(st1-ed1)-ed2]
		else if(st2 <= st1 && ed2 >= ed1){
			result.setStartDate(new Date(st1));
			result.setEndDate(new Date(ed1));
		}
		//(st1-[st2-ed1)-ed2]
		else if(st1 <= st2 && st2 <= ed1 && ed2 >= ed1){
			result.setStartDate(new Date(st2));
			result.setEndDate(new Date(ed1));
		}
		//[st2-(st1-ed2]-ed1)
		else if(st2 <= st1 && ed1 >= ed2 && st1 <= ed2){
			result.setStartDate(new Date(st1));
			result.setEndDate(new Date(ed2));
		}else {
			result.setStartDate(null);
			result.setEndDate(null);
		}
		return result;
	}
	

	
}
